package cn.edu.njuit.springbootfilmreview.controller;

import cn.edu.njuit.springbootfilmreview.dto.CommentInfo;
import cn.edu.njuit.springbootfilmreview.service.CommentInfoService;
import cn.edu.njuit.springbootfilmreview.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Author guzy
 * @Description 评论
 * @Date 2022/6/5 9:08
 **/

@RestController
@RequestMapping("api/comment-info")
@Api(value = "评论接口", tags = "评论接口")
public class CommentInfoController {

    @Autowired
    CommentInfoService commentInfoService;

    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "用户id", name = "userId", dataType = "Long", required = true),
            @ApiImplicitParam(value = "作品id", name = "workId", dataType = "Long", required = true),
            @ApiImplicitParam(value = "评论内容", name = "text", dataType = "String", required = true)})
    @ApiOperation(value = "添加评论")
    @PostMapping("add")
    public Result addCommentInfo(@RequestParam Long userId,
                                 @RequestParam Long workId,
                                 @RequestParam String text,
                                 @RequestParam String token) throws ParseException {
        CommentInfo commentInfo = new CommentInfo();
        commentInfo.setUserId(userId);
        commentInfo.setCommentTime(new Date());
        commentInfo.setText(text);
        commentInfo.setWorksId(workId);
        boolean b = commentInfoService.addCommentInfo(commentInfo);
        if (b) {
            return Result.success("评论添加成功");
        } else {
            return Result.failure("评论添加失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "评论id", name = "id", dataType = "Long", required = true)
    })
    @ApiOperation(value = "删除评论")
    @DeleteMapping("delete/{id}")
    public Result deleteCommentInfoById(@PathVariable Long id, @RequestParam String token) {
        boolean b = commentInfoService.deleteCommentInfoById(id);
        if (b) {
            return Result.success("评论删除成功");
        } else {
            return Result.failure("评论删除失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "第几页", name = "currentPage", dataType = "int", required = true),
            @ApiImplicitParam(value = "当前页多少条数据", name = "pageSize", dataType = "int", required = true),
            @ApiImplicitParam(value = "评论id", name = "commentId", dataType = "int")
    })
    @ApiOperation(value = "根据作品id查询评论")
    @GetMapping("getAllByCommentId/{currentPage}/{pageSize}")
    public Result getCommentInfoByWorksId(
            @PathVariable Integer currentPage,
            @PathVariable int pageSize,
            @RequestParam Long commentId,
            @RequestParam String token) {
        IPage<CommentInfo> commentInfoIPage = commentInfoService.getCommentInfoByWorksId(currentPage, pageSize, commentId);
        if (commentInfoIPage.getRecords().size() > 0) {
            return Result.success(commentInfoIPage);
        } else {
            return Result.failure("没有找到评论");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "第几页", name = "currentPage", dataType = "int", required = true),
            @ApiImplicitParam(value = "当前页多少条数据", name = "pageSize", dataType = "int", required = true),
            @ApiImplicitParam(value = "用户id", name = "userId", dataType = "int")
    })
    @ApiOperation(value = "根据用户id查询评论")
    @GetMapping("getAllByuserId/{currentPage}/{pageSize}")
    public Result getCommentInfoByUserId(
            @PathVariable Integer currentPage,
            @PathVariable int pageSize,
            @RequestParam Long userId,
            @RequestParam String token) {
        IPage<CommentInfo> commentInfoIPage = commentInfoService.getCommentInfoByUserId(currentPage, pageSize, userId);
        if (commentInfoIPage.getRecords().size() > 0) {
            return Result.success(commentInfoIPage);
        } else {
            return Result.failure("没有找到评论");
        }
    }
}
